package com.ui;

import com.base.BaseRefreshPresenter;
import com.bean.BannerBean;
import com.bean.BaseBean;
import com.bean.HomeArticleList;
import com.net.ApiService;
import com.net.RetrofitServiceManager;
import com.net.RxResponseObserver;
import com.net.RxSchedulers;

import java.util.ArrayList;
import java.util.List;

public class TestPresenter extends BaseRefreshPresenter<TestContract.View> implements TestContract.Presenter {
    @Override
    public void getArticle(final int page) {
        RetrofitServiceManager.getInstance().create(ApiService.class).getHomeArticleList(page)
                .compose(RxSchedulers.<BaseBean<HomeArticleList>>normalSchedulers())
                .compose(mView.<BaseBean<HomeArticleList>>bindToLife())
                .subscribe(new RxResponseObserver<BaseBean<HomeArticleList>>() {
                    @Override
                    public void onSuccess(BaseBean<HomeArticleList> homeArticleListBaseBean) {
                        List<HomeArticleList.DatasBean> datas = homeArticleListBaseBean.getData().getDatas();
                        if (page == 0 && datas.size() == 0) {
                            noData();
                        } else if (page == 0) {
                            mView.onGetArticle(datas);
                        } else {
                            mView.onLoadMore(datas);
                        }
                    }
                });
    }

    @Override
    public void getBannerList() {
        RetrofitServiceManager.getInstance().create(ApiService.class).getHomeBannerList()
                .compose(RxSchedulers.<BaseBean<List<BannerBean>>>normalSchedulers())
                .compose(mView.<BaseBean<List<BannerBean>>>bindToLife())
                .subscribe(new RxResponseObserver<BaseBean<List<BannerBean>>>() {
                    @Override
                    public void onSuccess(BaseBean<List<BannerBean>> bannerBeanBaseBean) {
                        List<String> images = new ArrayList<>();
                        List<String> title = new ArrayList<>();
                        for (BannerBean dataBean : bannerBeanBaseBean.getData()) {
                            images.add(dataBean.getImagePath());
                            title.add(dataBean.getTitle());
                        }
                        mView.onGetBannerList(images,title);
                    }
                });
    }
}
